package com.solve.solvezuul;

import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.solve.entity.BaseResponse;
import com.solve.utils.JWTUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter  {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {

        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String uri = request.getRequestURI();
        System.out.println(uri);
        //只过滤OPTIONS 请求
        if(uri.contains("user/login") || uri.contains("user/register")){
            return false;
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //向header中添加鉴权令牌
        RequestContext requestContext = RequestContext.getCurrentContext();

        //获取header
        HttpServletRequest request = requestContext.getRequest();
        String authorization = request.getHeader("Authorization");

        String username = "";
        String userId = "";
        //首先检查当前token是否为空
        if (StringUtils.isEmpty(authorization)){
            BaseResponse baseResponse = new BaseResponse(false,"token不能为空!","");
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
            requestContext.setResponseBody(JSON.toJSONString(baseResponse));
            requestContext.getResponse().setContentType("application/json;charset=UTF-8");
            return null;
        }
        try{
             username = JWTUtil.getUsername(authorization);
             userId = JWTUtil.getUserId(authorization);
        }catch (Exception e){
            BaseResponse baseResponse = new BaseResponse(false,"token信息违规篡改!","");
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
            requestContext.setResponseBody(JSON.toJSONString(baseResponse));
            requestContext.getResponse().setContentType("application/json;charset=UTF-8");
            return null;
        }

        if (!JWTUtil.verify(authorization, username,userId, JWTUtil.secret)) {
            BaseResponse baseResponse = new BaseResponse(false,"token失效!","");
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(401);
            requestContext.setResponseBody(JSON.toJSONString(baseResponse));
            requestContext.getResponse().setContentType("application/json;charset=UTF-8");
            return null;
        }

        if(!StringUtils.isEmpty(authorization)) {
            System.out.println("Authorization: " + authorization);
            requestContext.addZuulRequestHeader("Authorization", authorization);
        }
        return null;
    }

}
